



package jnpf.controller;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.annotation.JnpfField;
import jnpf.base.ActionResult;
import jnpf.base.entity.DictionaryDataEntity;
import jnpf.base.model.dictionarydata.DictionaryDataListVO;
import jnpf.base.model.dictionarydata.DictionaryDataModel;
import jnpf.base.model.dictionarydata.PageDictionaryData;
import jnpf.base.service.DictionaryDataService;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.base.UserInfo;
import jnpf.base.vo.DownloadVO;
import jnpf.config.ConfigValueUtil;
import jnpf.exception.DataException;
import jnpf.onlinedev.model.ExcelImFieldModel;
import jnpf.onlinedev.model.OnlineImport.ExcelImportModel;
import jnpf.onlinedev.model.OnlineImport.VisualImportModel;
import jnpf.permission.entity.UserEntity;
import jnpf.util.treeutil.ListToTreeUtil;
import jnpf.util.treeutil.SumTree;
import jnpf.util.treeutil.newtreeutil.TreeDotUtils;
import org.springframework.transaction.annotation.Transactional;
import jnpf.base.entity.ProvinceEntity;
import jnpf.model.mzgateway.*;
import jnpf.model.mzgateway.MzGatewayPagination;
import jnpf.entity.*;
import jnpf.util.*;
import jnpf.base.util.*;
import jnpf.base.vo.ListVO;
import jnpf.util.context.SpringContext;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.Cleanup;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jnpf.entity.MzGatewayEntity;
import jnpf.service.MzGatewayService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import jnpf.util.GeneraterSwapUtil;
import java.util.stream.Collectors;
import java.util.*;

import cn.xuyanwu.spring.file.storage.FileInfo;


/**
 *
 * mzGateway
 * @版本： V3.2.0
 * @版权： 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2024-03-05
 */
@Slf4j
@RestController
@Tag(name = "mzGateway" , description = "example")
@RequestMapping("/api/example/MzGateway")
public class MzGatewayController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;

    @Autowired
    private DictionaryDataService dictionaryDataService;

    @Autowired
    private MzGatewayService mzGatewayService;



    /**
     * 列表
     *
     * @param mzGatewayPagination
     * @return
     */
    @Operation(summary = "获取列表")
	@PostMapping("/getList")
    public ActionResult list(@RequestBody MzGatewayPagination mzGatewayPagination)throws IOException{
    //树形判断是否有参数，有参数不造树
        List<MzGatewayEntity> list= mzGatewayService.getList(mzGatewayPagination);
        //处理id字段转名称，若无需转或者为空可删除

    List<MzGatewayListVO> listVO=JsonUtil.getJsonToList(list,MzGatewayListVO.class);
    for(MzGatewayListVO mzGatewayVO:listVO){
        ActionResult info = info(mzGatewayVO.getId());
        Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());
                mzGatewayVO.setTitle_name(mzGatewayVO.getTitle());

                mzGatewayVO.setLeftbutton_name(mzGatewayVO.getLeftbutton());

                mzGatewayVO.setRightbutton_name(mzGatewayVO.getRightbutton());

                mzGatewayVO.setSchoolemblemimage_name(generaterSwapUtil.getFileNameInJson(mzGatewayVO.getSchoolemblemimage()));

                mzGatewayVO.setImprint_name(mzGatewayVO.getImprint());

                mzGatewayVO.setBackgroundimage_name(generaterSwapUtil.getFileNameInJson(mzGatewayVO.getBackgroundimage()));

                mzGatewayVO.setBackgroundsecondary_name(generaterSwapUtil.getFileNameInJson(mzGatewayVO.getBackgroundsecondary()));

                mzGatewayVO.setBackgroundthird_name(generaterSwapUtil.getFileNameInJson(mzGatewayVO.getBackgroundthird()));
                //查询数据字典项目类型子项
                List<DictionaryDataEntity> data = dictionaryDataService.getList("534298679746494597");
                List<DictionaryDataModel> voListVO = JsonUtil.getJsonToList(data, DictionaryDataModel.class);
                if(voListVO.size()==2){
                    for(DictionaryDataModel model:voListVO){
                        if(model.getFullName().equals("本科")){
                            mzGatewayVO.setUndergraduate(model.getId());
                        } else if (model.getFullName().equals("研究生")) {
                            mzGatewayVO.setPostgraduate(model.getId());
                        }
                    }
                }

		//子表数据转换
     }

         PageListVO vo=new PageListVO();
         vo.setList(listVO);
         PaginationVO page=JsonUtil.getJsonToBean(mzGatewayPagination,PaginationVO.class);
         vo.setPagination(page);
         return ActionResult.success(vo);

    }

    /**
     * 获取数据字典列表
     *
     * @param dictionaryTypeId   数据字典id
     * @return ignore
     */
    @Operation(summary = "获取数据字典列表")
    @Parameters({
            @Parameter(name = "dictionaryTypeId", description = "数据分类id", required = true)
    })
    public List<DictionaryDataModel> bindDictionary( String dictionaryTypeId) {
        List<DictionaryDataEntity> data = dictionaryDataService.getList(dictionaryTypeId);
        List<DictionaryDataModel> voListVO = JsonUtil.getJsonToList(data, DictionaryDataModel.class);
        return voListVO;
    }

    /**
     * 创建
     *
     * @param mzGatewayForm
     * @return
     */
    @PostMapping
    @Transactional
    @Operation(summary = "创建")
    public ActionResult create(@RequestBody @Valid MzGatewayForm mzGatewayForm) throws DataException {
        String b = mzGatewayService.checkForm(mzGatewayForm,0);
        if (StringUtil.isNotEmpty(b)){
         return ActionResult.fail(b + "不能重复");
        }
        String mainId =RandomUtil.uuId();

        UserInfo userInfo=userProvider.get();
		UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        GeneraterSwapUtil.swapDatetime(mzGatewayForm);
        MzGatewayEntity entity = JsonUtil.getJsonToBean(mzGatewayForm, MzGatewayEntity.class);
            entity.setId(mainId);
                mzGatewayService.save(entity);


        return ActionResult.success("创建成功");
}


    /**
    * 信息
    *
    * @param id
    * @return
    */
    @Operation(summary = "信息")
    @GetMapping("/{id}")
    public ActionResult<MzGatewayInfoVO> info(@PathVariable("id") String id){
        MzGatewayEntity entity= mzGatewayService.getInfo(id);
        MzGatewayInfoVO vo=JsonUtil.getJsonToBean(entity, MzGatewayInfoVO.class);

    //子表
    //副表
        return ActionResult.success(vo);
    }

    /**
    * 表单信息(详情页)
    *
    * @param id
    * @return
    */
    @Operation(summary = "表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<MzGatewayInfoVO> detailInfo(@PathVariable("id") String id){
        ActionResult info = info(id);
        Map<String,Object> dataMap = JsonUtil.entityToMap(info.getData());
        MzGatewayEntity entity= mzGatewayService.getInfo(id);
        MzGatewayInfoVO vo=JsonUtil.getJsonToBean(entity, MzGatewayInfoVO.class);

 //子表数据转换

 //附表数据转换

//添加到详情表单对象中
                vo.setTitle(vo.getTitle());

                vo.setLeftbutton(vo.getLeftbutton());

                vo.setRightbutton(vo.getRightbutton());

                vo.setSchoolemblemimage(generaterSwapUtil.getFileNameInJson(vo.getSchoolemblemimage()));

                vo.setImprint(vo.getImprint());

                vo.setBackgroundimage(generaterSwapUtil.getFileNameInJson(vo.getBackgroundimage()));

                vo.setBackgroundsecondary(generaterSwapUtil.getFileNameInJson(vo.getBackgroundsecondary()));

                vo.setBackgroundthird(generaterSwapUtil.getFileNameInJson(vo.getBackgroundthird()));


        return ActionResult.success(vo);
		}




   /**
    * 更新
    *
    * @param id
    * @param mzGatewayForm
    * @return
    */
    @PutMapping("/{id}")
    @Transactional
    @Operation(summary = "更新")
    public ActionResult update(@PathVariable("id") String id,@RequestBody @Valid MzGatewayForm mzGatewayForm) throws DataException {
        String b =  mzGatewayService.checkForm(mzGatewayForm,1);
        if (StringUtil.isNotEmpty(b)){
            return ActionResult.fail(b + "不能重复");
        }
        UserInfo userInfo=userProvider.get();
        MzGatewayEntity entity= mzGatewayService.getInfo(id);
        if(entity!=null){
            GeneraterSwapUtil.swapDatetime(mzGatewayForm);
            MzGatewayEntity subentity=JsonUtil.getJsonToBean(mzGatewayForm, MzGatewayEntity.class);
				boolean b1 = mzGatewayService.updateById(subentity);
				if (!b1){
				return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
				}
        return ActionResult.success("更新成功");
        }else{
            return ActionResult.fail("更新失败，数据不存在");
        }
    }



   /**
    * 删除
    *
    * @param id
    * @return
    */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id){
        MzGatewayEntity entity= mzGatewayService.getInfo(id);
        if(entity!=null){
            mzGatewayService.delete(entity);
        }
        return ActionResult.success("删除成功");
    }






}
